<template>
  <div class="swiper-container" ref="mySwiper">
    <div class="swiper-wrapper">
      <div
        v-for="carousel in bannerList"
        :key="carousel.id"
        class="swiper-slide"
      >
        <img :src="carousel.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from 'swiper'
export default {
  name: 'Carousel',
  props: {
    bannerList: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    bannerList: {
      immediate: true,
      deep: true,
      handler () {
        this.$nextTick(() => {
          new Swiper(this.$refs.mySwiper, {
            // direction: 'vertical', // 垂直切换选项
            loop: true, // 循环模式选项

            // 如果需要分页器
            pagination: {
              el: '.swiper-pagination',
              clickable: true
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },

            autoplay: {
              delay: 2000, //自动切换的时间间隔，单位ms
              disableOnInteraction: false //用户操作swiper之后，是否禁止autoplay
            },
          })
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>